package com.suep.game_chat.controller;

import com.suep.game_chat.entity.UserExperience;
import com.suep.game_chat.service.ExperienceService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userapi/experience")
@RequiredArgsConstructor
public class ExperienceController {

    private final ExperienceService experienceService;

    /**
     * 添加经验值
     * @param userId 用户ID
     * @param points 经验值点数
     * @param reason 经验值获取原因
     * @return 响应实体
     */
    @PostMapping("/add")
    public ResponseEntity<String> addExperience(
            @RequestParam String userId,
            @RequestParam int points,
            @RequestParam String reason) {
        boolean success = experienceService.addExperience(userId, points, reason);
        if (success) {
            return ResponseEntity.ok("经验值添加成功");
        } else {
            return ResponseEntity.badRequest().body("经验值添加失败");
        }
    }

    /**
     * 获取用户经验值历史
     * @param userId 用户ID
     * @return 经验值历史记录列表
     */
    @GetMapping("/history/{userId}")
    public ResponseEntity<List<UserExperience>> getUserExperienceHistory(@PathVariable String userId) {
        List<UserExperience> records = experienceService.getUserExperienceHistory(userId);
        return ResponseEntity.ok(records);
    }

    /**
     * 获取用户当前等级
     * @param userId 用户ID
     * @return 用户等级
     */
    @GetMapping("/level/{userId}")
    public ResponseEntity<Integer> getUserLevel(@PathVariable String userId) {
        int level = experienceService.getUserLevel(userId);
        return ResponseEntity.ok(level);
    }

    /**
     * 获取用户当前经验值
     * @param userId 用户ID
     * @return 用户经验值
     */
    @GetMapping("/current/{userId}")
    public ResponseEntity<Integer> getUserExperience(@PathVariable String userId) {
        int experience = experienceService.getUserExperience(userId);
        return ResponseEntity.ok(experience);
    }
} 